package com.googlecode.gwt4gae.client.menu;

import java.util.Arrays;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionService;
import com.extjs.gxt.ui.client.event.SourceSelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.googlecode.gwt4gae.client.ActionRegistry;
import com.googlecode.gwt4gae.client.AppEvents;
import com.googlecode.gwt4gae.client.BaseAction;
import com.googlecode.gwt4gae.client.BaseView;
import com.googlecode.gwt4gae.client.Gwt4gaeConstants;

public class MenuView extends BaseView {
	private ContentPanel westPanel;
	private MenuModel treeModel = null;
	private TabPanel tabPanel;
	private TreePanel<ModelData> treePanel;

	private TreeStore<ModelData> treeStore;

	public MenuModel getTreeModel() {
		return treeModel;
	}

	public void setTreeModel(MenuModel treeModel) {
		this.treeModel = treeModel;
	}

	public MenuView(BaseAction action) {
		super(action);
	}

	@Override
	protected void initialize() {
		SelectionService.get().addListener(
				new SelectionChangedListener<TreeModel>() {
					public void selectionChanged(
							SelectionChangedEvent<TreeModel> event) {
						List<TreeModel> sel = event.getSelection();
						if (sel.size() > 0) {
							TreeModel m = (TreeModel) event.getSelection().get(
									0);
							if (m != null && m instanceof MenuModel) {
								MenuModel menuModel = (MenuModel) m;
								AppEvent appEvent = new AppEvent(ActionRegistry
										.getActionEventType(menuModel.getUrl()));
								appEvent.setHistoryEvent(true);
								appEvent.setToken(menuModel.getUrl());
								appEvent.setData(menuModel);
								Dispatcher.forwardEvent(appEvent);
							}
						}
					}
				});
		createTabPanel();
	}

	private void createTabPanel() {
		tabPanel = new TabPanel();
		tabPanel.setMinTabWidth(70);
		tabPanel.setBorderStyle(false);
		tabPanel.setBodyBorder(false);
	}

	private void createTreeMenu() {
		treeStore = new TreeStore<ModelData>();
		treeStore.setKeyProvider(new ModelKeyProvider<ModelData>() {
			public String getKey(ModelData model) {
				return ((MenuModel) model).getName();
			}
		});
		treeStore.add(this.getTreeModel(), true);

		treePanel = new TreePanel<ModelData>(treeStore);
		treePanel.getStyle().setLeafIcon(IconHelper.createStyle("icon-list"));
		treePanel.setAutoLoad(true);
		treePanel.setDisplayProperty("name");

		SelectionService.get().addListener(
				new SourceSelectionChangedListener(treePanel
						.getSelectionModel()));
		SelectionService.get().register(treePanel.getSelectionModel());

		westPanel = Registry.get(Gwt4gaeConstants.WEST_PANEL);
		westPanel.setHeading("导航");
		westPanel.setLayout(new FitLayout());
		westPanel.add(treePanel);
		westPanel.syncSize();
	}

	@SuppressWarnings("unchecked")
	protected void handleEvent(AppEvent event) {
		EventType type = event.getType();
		MenuModel menuModel = event.getData();
		if (type == AppEvents.HIDE_PAGE) {
			treePanel.getSelectionModel().deselectAll();
		} else if (type == AppEvents.TAB_CHANGE) {
			if (Gwt4gaeConstants.WELCOME_ACTION_NAME.equals(menuModel.getUrl())) {
				treePanel.getSelectionModel().deselectAll();
			} else {
				treePanel.getSelectionModel().setSelection(
						(List) Arrays.asList(menuModel));
			}
		} else {
			this.setTreeModel(menuModel);
			createTreeMenu();
		}
	}

}
